<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.groupName" clearable placeholder="请输入小组名" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel>
        <el-button icon="el-icon-plus" type="primary" @click="handleAdd">添加</el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table border :height="height" v-loading="listLoading" :data="Dpmentlist" :element-loading-text="elementLoadingText">
      <el-table-column label="ID" prop="id" show-overflow-tooltip />
      <el-table-column label="小组名称" prop="groupName" show-overflow-tooltip />
      <el-table-column label="创建时间" prop="createTime" />
      <el-table-column label="备注" prop="memo" />
      <el-table-column label="排序" prop="sort" show-overflow-tooltip width="80px" />
      <el-table-column label="操作" show-overflow-tooltip width="300px">
        <template #default="{ row }">
          <el-button type="warning" size="mini" @click="handleEdit(row)">编辑</el-button>
          <el-button type="info" size="mini" @click="GroupAddScore(row)">小组讨论加分</el-button>
          <el-button :disabled="row.id === 2" type="danger" size="mini" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { thirteenBitTimestamp } from '@/utils/index.js'
  import { SelActivitygroup, DelActivitygroup } from '@/api/userManagement'
  import Edit from './components/GroupMentEdit'

  export default {
    name: 'UserManagement',
    components: { Edit },
    data() {
      return {
        listLoading: true,
        total: 0,
        Dpmentlist: [],
        elementLoadingText: '正在加载...',
        queryForm: {
          groupName: '',
        },
        timeOutID: null,
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },

    created() {
      this.fetchData()
    },

    beforeDestroy() {
      clearTimeout(this.timeOutID)
    },
    methods: {
      //添加
      handleAdd() {
        this.$refs['edit'].showEdit()
      },
      //编辑
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      GroupAddScore(row) {
        this.$refs['edit'].showEdit(row, 1)
      },
      //删除
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await DelActivitygroup({ groupId: row.id })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        }
      },
      turntime(time) {
        return thirteenBitTimestamp(time)
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        try {
          const { data } = await SelActivitygroup(this.queryForm)
          this.Dpmentlist = data
          this.timeOutID = setTimeout(() => {
            this.listLoading = false
          }, 300)
        } catch (error) {
          console.error(error)
          this.listLoading = false
        }
      },
    },
  }
</script>
